﻿using AmazeOn.Models;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;

namespace AmazeOn.Content
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class CartViewModel : ViewModelBase
    {        
        public Item ToRemove { get; set; }
        
        private List<Item> cart;

        public List<Item> Cart
        {
            get
            {
                return MainPageViewModel.magazin.Cart.ItemsInCart;
            }
            set
            {
                this.cart = value;
            }
        }
        /// <summary>
        /// Initializes a new instance of the CartViewModel class.
        /// </summary>
        public CartViewModel()
        {
            goToStore = new RelayCommand(() => GoToStore());
            removeFromCart = new RelayCommand(() => RemoveFromCart());
            purchase = new RelayCommand(() => Purchase());
        }

        public double TotalPrice
        {
            get
            {
                return MainPageViewModel.magazin.Cart.ItemsInCart.Sum(i => i.Price);
            }
        }

        public RelayCommand goToStore
        {
            get;
            private set;
        }

        public RelayCommand removeFromCart
        {
            get;
            private set;
        }

        public RelayCommand purchase
        {
            get;
            private set;
        }

        private void Purchase()
        {
            foreach (var item in MainPageViewModel.magazin.Cart.ItemsInCart)
            {
                string storageFile = "../../AdminLog/log.txt";
                StreamWriter writer = new StreamWriter(storageFile, true);
                using (writer)
                {
                    writer.WriteLine(MainPageViewModel.magazin.CurrentUser.Username);
                    writer.WriteLine(MainPageViewModel.magazin.CurrentUser.Address);
                    writer.WriteLine(item.Model);
                    if (item.Promotion == null)
                    {
                        writer.WriteLine(item.Price);
                    }
                    else
                    {
                        writer.WriteLine(item.Promotion.PromoPrice);
                    }
                }
            }

            MainPageViewModel.magazin.Cart.EmptyCart();

            System.Windows.MessageBox.Show("Your order had been sent to the administrator");

            RaisePropertyChanged("Cart");
            RaisePropertyChanged("TotalPrice"); 
        }

        private void GoToStore()
        {
            var msg = new GoToPageMessage() { PageName = "StoreView" };
            Messenger.Default.Send<GoToPageMessage>(msg);              
        }

        private void RemoveFromCart()
        {
            MainPageViewModel.magazin.Cart.RemoveItem(ToRemove);            
            RaisePropertyChanged("Cart");
            RaisePropertyChanged("TotalPrice");                        
        }
    }
}